from django.shortcuts import render, redirect

from dept.models import DeptInfo

# Create your views here.
def dept_show(request):
    if request.GET.get("del_id"):
        DeptInfo.objects.filter(id=request.GET.get("del_id")).delete()
        return redirect('/dept/')
    else:
        dept_list = DeptInfo.objects.all()
        return render(request, "dept_show.html", {"dept_list": dept_list})

def dept_add(request):
    if request.method == "POST":
        DeptInfo.objects.create(name=request.POST["name"])
        return redirect('/dept/')
    elif request.method == "GET":
        return render(request, "dept_add.html")


def dept_update(request):
    if request.method == "POST":
        dept_id = request.POST.get("upd_id")
        name = request.POST.get("name")
        DeptInfo.objects.filter(id=dept_id).update(name=name)
        return redirect('/dept/')
    elif request.method == "GET":
        dept_id = request.GET.get("upd_id")
        return render(request, "dept_update.html", {"udp_id": dept_id})
    return None